<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      :page.sync="page"
      :permission="permissionList"
      :before-open="beforeOpen"
      v-model="form"
      ref="crud"
      @row-update="rowUpdate"
      @row-save="rowSave"
      @row-del="rowDel"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template slot="menuLeft">
        <el-button type="warning" size="small" icon="el-icon-download" @click="handleExport">导出</el-button>
      </template>
      <template slot="menuRight">
        <user-table-config :option="option" @confirm="(val) => handleSetUserConfig(val, pageCode)"></user-table-config>
      </template>
      <template slot-scope="{ row }" slot="menu">
        <el-button type="text" icon="el-icon-view" size="small" plain style="border: 0; background-color: transparent !important" @click.stop="handleView(row)">详情 </el-button>
      </template>
    </avue-crud>

    <el-dialog
      append-to-body
      :close-on-click-modal="false"
      title="成品入库单详情"
      :visible.sync="showDialog"
      v-if="showDialog"
      custom-class="finishWarehouseManage"
      width="60%"
      top="7vh"
    >
      <div class="common-modal-sub-title">
        <i class="el-icon-vertical-bar avue-group__icon"></i>
        <h1 class="avue-group__title">基本信息</h1>
      </div>
      <avue-form :option="baseOption" v-model="baseForm" ref="form"></avue-form>
      <div class="common-modal-sub-title">
        <i class="el-icon-vertical-bar avue-group__icon"></i>
        <h1 class="avue-group__title">详情</h1>
      </div>
      <avue-crud :option="sheetOption" :data="sheetData" :permission="permissionList" ref="addTableCrud" :key="timer"> </avue-crud>
      <div slot="footer" class="avue-dialog__footer">
        <el-button size="small" icon="el-icon-circle-close" @click="showDialog = false">取消</el-button>
      </div>
    </el-dialog>

    <export-comp ref="exportComp" page-api-url="mes-warehouse/finished-inbound-order/page" :option="option" :page="page" :query="exportParams" file-name="成品入库单">
    </export-comp>
  </basic-container>
</template>

<script>
import { getList, getDetail } from '@/api/warehouse/finish-warehouse-manage';
import { mapGetters } from 'vuex';
import userTableConfig from '@/components/user-table-config';
import userConfig from '@/mixins/userConfig';
import exportComp from '@/components/export-comp';

export default {
  components: {
    userTableConfig,
    exportComp
  },
  mixins: [userConfig()],
  data() {
    return {
      pageCode: 'FINISH_WAREHOUSE_MANAGE',
      form: {},
      exportParams: {},
      loading: false,
      showDialog: false,
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      data: [],
      sheetData: [],
      option: {
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchMenuPosition: 'right',
        searchMenuSpan: 19,
        searchLabelWidth: 100,
        dialogClickModal: false,
        border: true,
        height: 'calc(100vh - 300px)',
        viewBtn: false,
        addBtn: false,
        selection: true,
        printBtn: false,
        editBtn: false,
        delBtn: false,
        index: true,
        indexLabel: '序号',
        columnBtn: false,
        menu: true,
        searchSpan: 5,
        column: [
          {
            label: '成品入库单',
            prop: 'inboundOrderNo',
            search: true,
            searchOrder: 1
          },
          {
            label: '产品编码',
            prop: 'productPartNo',
            hide: true
          },
          {
            label: '生产工单',
            prop: 'workOrderNo',
            hide: true
          },
          {
            label: '状态',
            prop: 'status',
            type: 'select',
            dicData: [
              {
                label: '正常',
                value: 1
              },
              {
                label: '完成',
                value: 2
              },
              {
                label: '取消',
                value: 3
              },
              {
                label: '关闭',
                value: 4
              }
            ]
          },
          {
            label: '单据来源',
            prop: 'sourceType',
            type: 'select',
            dicUrl: '/api/blade-system/dict/dictionary?code=sheet_source',
            props: {
              label: 'dictValue',
              value: 'dictKey'
            }
          },
          {
            label: '最后同步时间',
            prop: 'lastSyncTime',
            rules: [
              {
                required: true,
                message: '最后同步时间',
                trigger: 'blur'
              }
            ]
          }
        ]
      },
      sheetOption: {
        tip: false,
        searchShow: true,
        searchSpan: 4,
        searchMenuSpan: 6,
        dialogClickModal: false,
        height: '400',
        border: true,
        addBtn: false,
        editBtn: false,
        viewBtn: false,
        delBtn: false,
        columnBtn: false,
        cancelBtn: false,
        saveBtn: false,
        selection: false,
        menu: false,
        menuWidth: 100,
        page: false,
        index: true,
        indexLabel: '序号',
        refreshBtn: false,
        expandBtn: false,
        column: [
          {
            label: '产品编码',
            prop: 'productPartNo'
          },
          {
            label: '产品名称',
            prop: 'productPartName'
          },
          {
            label: '产品型号',
            prop: 'productModel'
          },
          {
            label: '工单号',
            prop: 'workOrderNo'
          },
          {
            label: '生产批次',
            prop: 'lotNo'
          },
          {
            label: '计划入库数量',
            prop: 'planInboundQuantity'
          },
          {
            label: '已入库数量',
            prop: 'inboundQuantity'
          },
          {
            label: '待入库数量',
            prop: 'preInboundQuantity'
          },
          {
            label: '状态',
            prop: 'status',
            type: 'select',
            dicData: [
              {
                label: '正常',
                value: 1
              },
              {
                label: '完成',
                value: 2
              },
              {
                label: '取消',
                value: 3
              },
              {
                label: '关闭',
                value: 4
              }
            ]
          }
        ]
      },
      baseOption: {
        emptyBtn: false,
        submitBtn: false,
        detail: true,
        labelWidth: 120,
        column: [
          { label: '成品入库单', prop: 'inboundOrderNo' },
          {
            label: '状态',
            prop: 'status',
            type: 'select',
            dataType: 'string',
            dicData: [
              {
                label: '正常',
                value: 1
              },
              {
                label: '完成',
                value: 2
              },
              {
                label: '取消',
                value: 3
              },
              {
                label: '关闭',
                value: 4
              }
            ]
          }
        ]
      }
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.$structuralSplit();
    });
  },
  computed: {
    ...mapGetters(['permission']),
    permissionList() {
      return {
        viewBtn: this.vaildData(this.permission.finishWarehouseManage_view, false)
      };
    }
  },
  methods: {
    handleExport() {
      this.$refs.exportComp.visible = true;
      this.exportParams = this.query;
    },
    rowSave(row, done, loading) {
      add(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
          done();
        },
        (error) => {
          loading();
          window.console.log(error);
        }
      );
    },
    rowUpdate(row, index, done, loading) {
      update(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
          done();
        },
        (error) => {
          loading();
          console.log(error);
        }
      );
    },
    rowDel(row) {
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
        });
    },
    beforeOpen(done, type) {
      if (['edit', 'view'].includes(type)) {
        // getSheetNoPack(this.form.id).then((res) => {
        //   this.form = res.data.data;
        // });
      }
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query))
        .then((res) => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.selectionClear();
        })
        .finally(() => {
          this.loading = false;
        });
    },
    handleView(row) {
      this.showDialog = true;
      this.baseForm = structuredClone(row);
      getDetail(row.inboundOrderNo).then((res) => {
        let data = res.data.data;
        this.sheetData = data;
      });
    }
  }
};
</script>

<style lang="scss" scoped>
::v-deep .finishWarehouseManage {
  .el-dialog__body {
    padding: 0 20px 20px;
  }
  .avue-crud__menu {
    display: none;
  }
  .common-modal-sub-title {
    display: flex;
    align-items: center;
    font-size: 16px;
    font-weight: bold;
    margin: 10px 0;
  }
}
</style>
